<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Support\Facades\DB;

class Tag extends Authenticatable
{
    use Notifiable,HasRoles;
    protected $fillable = ['name','sort'];


    public function getCustomerList($where,$search,$page,$limit)
    {

        $result =  DB::table('v_customer_field')
            ->where(function($query) use($search) {
                if (!empty($search['customer_name'])) {
                    $query->where('jin_customer_name', 'like', '%' . $search['customer_name'] . '%');
                }
                if (!empty($search['jin_customer_code'])) {
                    $query->where('jin_customer_name', $search['jin_customer_code']);
                }
            })
            ->where($where)
            ->select('*')
            ->orderBy('id','desc')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('v_customer_field')
            ->where(function($query) use($search) {
                if (!empty($search['jin_customer_name'])) {
                    $query->where('jin_customer_name', 'like', '%' . $search['jin_customer_name'] . '%');
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }




    public function getSupplierList($where,$search,$page,$limit)
    {

        $result =  DB::table('v_supplier_field')
            ->where(function($query) use($search) {
                if (!empty($search['customer_name'])) {
                    $query->where('jin_supplier_name', 'like', '%' . $search['customer_name'] . '%');
                }
                if (!empty($search['jin_supplier_code'])) {
                    $query->where('jin_supplier_code', $search['jin_supplier_code']);
                }
            })
            ->where($where)
            ->select('*')
            ->orderBy('id','desc')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('v_supplier_field')
            ->where(function($query) use($search) {
                if (!empty($search['customer_name'])) {
                    $query->where('jin_supplier_name', 'like', '%' . $search['customer_name'] . '%');
                }
                if (!empty($search['jin_supplier_code'])) {
                    $query->where('jin_supplier_code', $search['jin_supplier_code']);
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }

    public function getOneCustomerData($table,$where)
    {
        $result = DB::table($table)
            ->select('*')
            ->where($where)
            ->first();
        return self::convertObjectToArray($result);
    }


    protected static function convertObjectToArray($objects): array {
        if (is_array($objects)) {
            foreach ($objects as &$oneObject) {
                $oneObject = (array)$oneObject;
            }
        } elseif (is_object($objects)) {
            if ($objects instanceof Collection) {
                $objects = self::convertObjectToArray($objects->toArray());
            } else {
                $objects = (array)$objects;
            }
        }
        return $objects;
    }

}
